package com.gxkj.drm.controllers;


import com.gxkj.drm.constant.WebConstant;
import com.gxkj.drm.dto.CodeMsg;
import com.gxkj.drm.entitys.PushUser;
import com.gxkj.drm.entitys.PushUserOperateLog;
import com.gxkj.drm.entitys.ResourceManage;
import com.gxkj.drm.service.ResourceManageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController extends BaseController {

    @Resource
    private ResourceManageService resourceManageService;

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    //根据ip地址推送内容
    private static final String SHOW_RESOURCE_IP = "show_resource_ip";

    private static final String SHOW_RESOURCE_IP_FILE = SHOW_RESOURCE_IP + ".ftl";

    private static final String SHOW_RESOURCE_IP_MAPPING = "/" + SHOW_RESOURCE_IP;

    //根据属性推送内容
    private static final String SHOW_RESOURCE_ATTRIBUTE = "show_resource_attribute";

    private static final String SHOW_RESOURCE_ATTRIBUTE_FILE = SHOW_RESOURCE_ATTRIBUTE + ".ftl";

    private static final String SHOW_RESOURCE_ATTRIBUTE_MAPPING = "/" + SHOW_RESOURCE_ATTRIBUTE;

    //根据ip地址移除推送服务
    private static final String SHOW_REMOVE_IP = "show_remove_ip";

    private static final String SHOW_REMOVE_IP_FILE = SHOW_REMOVE_IP + ".ftl";

    private static final String SHOW_REMOVE_IP_MAPPING = "/" + SHOW_REMOVE_IP;

    //查看用户操作记录
    private static final String SHOW_USER_RECORD = "show_user_record";

    private static final String SHOW_USER_RECORD_FILE = SHOW_USER_RECORD + ".ftl";

    private static final String SHOW_USER_RECORD_MAPPING = "/" + SHOW_USER_RECORD;

    //登录页
    private static final String LOGIN_FILE = "login.ftl";


    /**
     * 主页框架
     * @param model
     * @return
     */
    @RequestMapping(value = WebConstant.PATH_HOME)
    public String showBase(Model model) {
        model.addAttribute("url", WebConstant.URL_INDEX);
        return WebConstant.PAGE_HOME;
    }


    /**
     * 根据属性显示资源属性
     * @param request
     * @param model
     * @param cluster
     * @param attribute
     * @return
     */
    @RequestMapping(value = SHOW_RESOURCE_ATTRIBUTE_MAPPING)
    public String showAttributeResource(HttpServletRequest request, Model model, String cluster, String attribute) {
        PushUser user = this.getUser(request);

        if (user != null) {
            List<ResourceManage> resourceManageList = new ArrayList<ResourceManage>();
            if (StringUtils.isNotBlank(attribute) && StringUtils.isNotBlank(cluster)) {
                resourceManageList = this.resourceManageService.getResourceManageByAttribute(cluster, attribute);
            }

            model.addAttribute("url", SHOW_RESOURCE_ATTRIBUTE_FILE);
            model.addAttribute("cluster", cluster);
            model.addAttribute("attribute", attribute);
            model.addAttribute("resourceManageList", resourceManageList);
        } else {
            model.addAttribute("url", LOGIN_FILE);
        }

        model.addAttribute("menu", SHOW_RESOURCE_ATTRIBUTE);
        model.addAttribute("user", user);

        return "/base";
    }

    /**
     * 根据ip显示资源属性
     * @param model
     * @param ip
     * @return
     */
    @RequestMapping(value = SHOW_REMOVE_IP_MAPPING)
    public String showRemoveIpResource(HttpServletRequest request, Model model, String cluster, String ip) {
        PushUser user = this.getUser(request);

        if (user != null) {
            model.addAttribute("url", SHOW_REMOVE_IP_FILE);
            model.addAttribute("cluster", cluster);
            model.addAttribute("ip", ip);
        } else {
            model.addAttribute("url", LOGIN_FILE);
        }

        model.addAttribute("menu", SHOW_REMOVE_IP);
        model.addAttribute("user", user);

        return "/base";
    }

    /**
     * 根据ip地址移除推送服务
     * @param model
     * @param ip
     * @return
     */
    @RequestMapping(value = SHOW_RESOURCE_IP_MAPPING)
    public String showIpResource(HttpServletRequest request, Model model, String cluster, String ip) {
        PushUser user = this.getUser(request);

        if (user != null) {
            List<ResourceManage> resourceManageList = new ArrayList<ResourceManage>();
            if (StringUtils.isNotBlank(ip) && StringUtils.isNotBlank(cluster)) {
                resourceManageList = this.resourceManageService.getResourceManageByIp(cluster, ip);
            }
            model.addAttribute("url", SHOW_RESOURCE_IP_FILE);
            model.addAttribute("cluster", cluster);
            model.addAttribute("ip", ip);
            model.addAttribute("resourceManageList", resourceManageList);
        } else {
            model.addAttribute("url", LOGIN_FILE);
        }

        model.addAttribute("menu", SHOW_RESOURCE_IP);
        model.addAttribute("user", user);

        return "/login";
    }

    /**
     * 显示用户操作记录信息
     * @param request
     * @return
     */
    @RequestMapping(value = SHOW_USER_RECORD_MAPPING)
    public String showUserRecord(HttpServletRequest request, Model model) {
        PushUser user = this.getUser(request);

        if (user != null) {
            List<PushUserOperateLog> pushUserOperateLogList = this.userService.getUserOperateLogList(user);

            model.addAttribute("url", SHOW_USER_RECORD_FILE);
            model.addAttribute("userOperateLogList", pushUserOperateLogList);
        } else {
            model.addAttribute("url", LOGIN_FILE);
        }

        model.addAttribute("menu", SHOW_USER_RECORD);
        model.addAttribute("user", user);

        return "/base";
    }

//    @Permission(needLogin = false)
    @ResponseBody
    @RequestMapping(value = "/conf", method = { RequestMethod.GET })
    public CodeMsg conf() {
        CodeMsg result = CodeMsg.SUCCESS;
        Map<String, String> confMap = new HashMap<>();
        //confMap.put("env", DrmEnv.getEnvironment());
        result.setData(confMap);
        logger.info("env={}", confMap.get("env"));

        return result;
    }

    @PostConstruct
    private void init() {
        //logger.info("server is start, env={}", DrmEnv.getEnvironment());
    }

    /**
     * 更新记录
     * @param model
     * @return
     */
    @RequestMapping(value = WebConstant.PATH_UPDATE_LOG)
    public String updateLog(Model model) {
        model.addAttribute("url", WebConstant.URL_UPDATE_LOG);
        return WebConstant.PAGE_HOME;
    }
}
